<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2014.09.20.
   广告管理控制器($$)*/

!defined('Q_PATH') && exit;

class AdController extends AController{

	public function filter_(&$arrMap){
		$arrMap['A.ad_note']=array('like',"%".Q::G('ad_note')."%");
		$arrMap['A.ad_name']=array('like',"%".Q::G('ad_name')."%");

		// 数据位
		$nIsData=Q::G('adposition_isdata');
		if($nIsData!==null && $nIsData!=''){
			$arrMap['C.adposition_isdata']=$nIsData;
		}
		
		// 广告类型
		$sType=Q::G('ad_type');
		if($sType!==null && $sType!=''){
			$arrMap['A.ad_type']=$sType;
		}

		// 广告时间
		$sAdstarttime=Q::G('ad_starttime');
		$sAdendtime=Q::G('ad_endtime');
		if($sAdstarttime!==null && $sAdstarttime!=''){
			$arrMap['A.ad_starttime']=array('egt',strtotime($sAdstarttime));
		}
		if($sAdendtime!==null && $sAdendtime!=''){
			$arrMap['A.ad_endtime']=array('elt',strtotime($sAdendtime));
		}
		
		// 添加时间
		$this->getTime_('A.create_dateline',$arrMap);
	}

	protected function sqljoin_(){
		return "->join('".Q::C('DB_PREFIX')."adposition AS C','C.adposition_name,C.adposition_code,C.adposition_isdata','A.adposition_id=C.adposition_id')";
	}

	public function getSites($sSite){
		return Core_Extend::getSites($sSite);
	}

	protected function AInsertObject_($oModel){
		$this->AUpdateObject_($oModel);
	}
	
	protected function AUpdateObject_($oModel){
		$arrData[$_POST['ad_type']]=$_POST['ad_data'][$_POST['ad_type']];
		$oModel->ad_setting=json_encode($arrData);
		if($_POST['ad_starttime']){
			$oModel->ad_starttime=strtotime($_POST['ad_starttime']);
		}
		if($_POST['ad_endtime']){
			$oModel->ad_endtime=strtotime($_POST['ad_endtime']);
		}
		if($_POST['ad_siteids']==''){
			$_POST['ad_siteids']=0;
		}
	}

	protected function AEditObject_($oModel){
		$this->assign('arrAddata',json_decode($oModel->ad_setting,true));
	}

	public function bIndex_(){
		Core_Extend::loadCache('app');
		$this->assign('arrApps',$GLOBALS['_cache_']['app']);
	}

	public function preview(){
		$nId=intval(Q::G('id'));
		if(!$nId){
			exit('<span style="color:red;">请求失败</span>');
		}

		$arrAd=Model::F_('ad','ad_id=?',$nId)->getOne();
		if(empty($arrAd['ad_id'])){
			exit('<span style="color:red;">请求失败</span>');
		}

		$sHtml=<<<WINDSFORCE
<div class="adminpreview_box">
	<div class="adminpreview_title">广告预览&nbsp;&nbsp;&nbsp;
		<a title="点击关闭" href="javascript:void(0);" onclick="closePreviewad('{$nId}');" title="关闭">x</a>
	</div>
	<div class="module_content">{content}</div>
</div>
WINDSFORCE;
		$sCode='';

		$arrAd=Core_Extend::getAdlink($arrAd);
		switch($arrAd['type']){
			case 'text':
				$sCode="<a href=\"{$arrAd['link']}\" target=\"_blank\" style=\"{$arrAd['style']}\">{$arrAd['title']}</a>";
				break;
			case 'image':
				$sCode="<a href=\"{$arrAd['link']}\" target=\"_blank\" title=\"{$arrAd['title']}\"><img src=\"{$arrAd['logo']}\" alt=\"{$arrAd['title']}\" style=\"max-width:500px;\"/></a>";
				break;
			case 'code':
				$sCode=htmlspecialchars($arrAd['html']);
			case 'flash':
				$sCode="<object style=\"padding-left:0px;padding-top: 5px;\" type='application/x-shockwave-flash' data='{$arrAd['file']}' width='400' height='200'>
					<param name='wmode' value='transparent' />
					<param name='movie' value='{$arrAd['file']}' />
				</object>";
				break;
		}

		echo str_replace('{content}',$sCode,$sHtml);
		exit();
	}

	public function getDataType($nType){
		switch($nType){
			case 1:
				return '<span style="color:#ED145B;">数据</span>';
				break;
			default:
				return '<span style="color:#0DD07F;">广告</span>';
				break;
		}
	}

	protected function aInsert($nId=null){
		$this->setAvip($nId);
	}
	
	protected function aUpdate($nId=null){
		$this->aInsert($nId);
	}
	
	protected function aForeverdelete_deep($sId){
		$arrId=explode(',',$sId);
		foreach($arrId as $nId){
			$this->aInsert($nId);
		}
	}

	protected function aForeverdelete($sId){
		$this->aForeverdelete_deep($sId);
	}

	protected function aForbid(){
		$nId=Q::G('id');
		$this->aInsert($nId);
	}
	
	protected function aResume(){
		$this->aForbid();
	}

	private function setAvip($nId=0){
		$nId=intval($nId);
		if(empty($nId)){
			return;
		}

		$nCpId=Model::F_('ad','ad_id=?',$nId)->getColumn('jiajucompany_id');
		if(empty($nCpId)){
			return;
		}

		$arrAd=Model::F_('ad')
			->where(array('jiajucompany_id'=>$nCpId,'ad_status'=>1,'ad_ispreset'=>0))
			->setColumns('ad_id')
			->getOne();

		if(!empty($arrAd['ad_id'])){
			Model::M_('jiajucompany')->updateWhere(array('jiajucompany_isavip'=>1),'jiajucompany_id=?',$nCpId);
		}else{
			Model::M_('jiajucompany')->updateWhere(array('jiajucompany_isavip'=>0),'jiajucompany_id=?',$nCpId);
		}
	}

}
